package dao

import (
	"haiqiu-go-admin/entity"
	"log"
)

func ConfigSelectByPrimaryKey(id int) (entity.SysConfig, error) {
	row := DB.QueryRow("select * from sys_config where id = ?", id)
	var sysConfig entity.SysConfig
	err := row.Scan(
		&sysConfig.Id,
		&sysConfig.Name,
		&sysConfig.Title,
		&sysConfig.Logo,
		&sysConfig.SideTheme,
		&sysConfig.Color,
		&sysConfig.Bottom,
		&sysConfig.ShowSettings,
		&sysConfig.TopNav,
		&sysConfig.TagsView,
		&sysConfig.FixedHeader,
		&sysConfig.SidebarLogo,
		&sysConfig.DynamicTitle,
		&sysConfig.ManyLogin,
		&sysConfig.Status,
		&sysConfig.Sort,
		&sysConfig.Register,
		&sysConfig.TokenTime,
		&sysConfig.Background,
		&sysConfig.Captcha,
		&sysConfig.CreateTime,
		&sysConfig.UpdateTime,
	)
	if err != nil {
		return sysConfig, err
	}
	return sysConfig, nil
}

func SysConfigUpdateById(sysConfig *entity.SysConfig) (int64, error) {
	stmt, err := DB.Prepare("update sys_config set name = ?,title=?,logo=?,side_theme=?,color=?,bottom=?," +
		"show_settings=?,top_nav=?,tags_view=?,fixed_header=?,sidebar_logo=?," +
		"dynamic_title=?,many_login=?,status=?,sort=?,register=?,token_time=?,background=?,captcha=?," +
		"create_time=?,update_time=? where id = ?")
	if err != nil {
		log.Fatal(err)
	}
	exec, err := stmt.Exec(sysConfig.Name, sysConfig.Title, sysConfig.Logo, sysConfig.SideTheme, sysConfig.Color, sysConfig.Bottom,
		sysConfig.ShowSettings, sysConfig.TopNav, sysConfig.TagsView, sysConfig.FixedHeader, sysConfig.SidebarLogo,
		sysConfig.DynamicTitle, sysConfig.ManyLogin, sysConfig.Status, sysConfig.Sort, sysConfig.Register, sysConfig.TokenTime,
		sysConfig.Background, sysConfig.Captcha, sysConfig.CreateTime, sysConfig.UpdateTime, sysConfig.Id)
	if err != nil {
		log.Fatal(err)
	}
	lastId, err := exec.LastInsertId()
	if err != nil {
		log.Fatal(err)
	}
	rowCnt, err := exec.RowsAffected()
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("ID = %d, affected = %d\n", lastId, rowCnt)
	return rowCnt, nil
}
